package com.rytong.tools.utils;

import com.rytong.tools.R;
import com.rytong.tools.ui.BaseView;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

/**
 * HomeKeyRroadCastReceiver.java 监听home键，采用广播形式，避免某些机型无法监听的问题
 * @author cheng.l 2013-10-15 下午01:52:23
 */
public class HomeKeyBroadCastReceiver extends BroadcastReceiver{
	static final String SYSTEM_REASON = "reason";
	//home键
	static final String SYSTEM_HOME_KEY = "homekey";
	//long home键
	static final String SYSTEM_RECENT_APPS = "recentapps";

	private Context context;
	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		this.context = context;
		if(AppAntiService.isPressHomeKey)
			return;
		String action = intent.getAction();
		if(null != action && action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)){
			String reason = intent.getStringExtra(SYSTEM_REASON);
			if(null != reason){
				if(reason.equals(SYSTEM_HOME_KEY)){
					AppAntiService.isPressHomeKey = true;
					context.unregisterReceiver(BaseView.keyReceiver);
					//处理按下home键的方法
					handler.sendEmptyMessageDelayed(0, 1000);
				}else if(reason.equals(SYSTEM_RECENT_APPS)){
					//监听到长按home键的处理方法
				}
			}
		}
	}
	
	private Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch(msg.what){
			case 0:
				if(null != context){
					Toast.makeText(context, R.string.app_warn_message, Toast.LENGTH_LONG).show();
				}
				break;
			}
			super.handleMessage(msg);
		}
	};
}
